﻿using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interop;

namespace LS.Screening.UI.Common.Controls
{
    public class CommonWindow<VM> : ReactiveWindow<VM> where VM : ViewModel
    {
        /// <summary>
        /// WPF窗体最大化、最小化、关闭按钮的隐藏和禁用
        /// </summary>
        [Reactive]
        public bool DisableFlag { get; set; } = false;

        /// <summary>
        /// 是否禁用关闭按钮
        /// </summary>
        [Reactive]
        public bool DisableClosing { get; set; } = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        public CommonWindow()
        {
            var viewModel = ServiceHelper.GetService<VM>();
            this.DataContext = viewModel;
            this.ViewModel = viewModel;
            this.Loaded += Window_Loaded;
            this.Unloaded += Window_Unloaded;
        }

        /// <summary>
        /// Window加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModel?.Init();
            DisableMaxmizebox(DisableFlag);

            //统一消息处理
            MessageBus.Current.Listen<string>("Notice").Subscribe(message =>
            {
                MessageBox.Show(message);
            });
        }

        /// <summary>
        /// Window卸载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            ViewModel?.Unload();
        }

        /// <summary>
        /// WPF窗体最大化、最小化、关闭按钮的隐藏和禁用
        /// </summary>
        /// <param name="isDisable"></param>
        private void DisableMaxmizebox(bool isDisable)
        {
            int GWL_STYLE = -16;
            int WS_MAXIMIZEBOX = 0x00010000;
            int WS_MINIMIZEBOX = 0x00020000;
            int SWP_NOSIZE = 0x0001;
            int SWP_NOMOVE = 0x0002;
            int SWP_FRAMECHANGED = 0x0020;
            IntPtr handle = new WindowInteropHelper(this).Handle;
            int nStyle = WinUser32.GetWindowLong(handle, GWL_STYLE);
            if (isDisable)
            {
                nStyle &= ~(WS_MAXIMIZEBOX);
                nStyle &= ~(WS_MINIMIZEBOX);
            }
            else
            {
                nStyle |= WS_MAXIMIZEBOX;
                nStyle |= WS_MINIMIZEBOX;
            }
            WinUser32.SetWindowLong(handle, GWL_STYLE, nStyle);
            WinUser32.SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
        }

        /// <summary>
        /// Window关闭事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (DisableClosing)
            {
                //禁用关闭按钮
                e.Cancel = true;
            }
        }
    }
}
